﻿using Quartz;
using SimulationCenter.Scripts;
using SimulationCenter.Tasks;
using SimulationCenter.Tasks.Svrs;
using System.Threading.Tasks;

namespace SimulationCenter.Jobs
{
    public class ScDeviceJob: IJob
    {
        public const string ScTaskId = "ScTaskId";
        public const string Device = "Device";

        private int _scTaskId;
        private string _device;

        private readonly ITaskSvr _taskSvr;
        private readonly ScriptManager _scriptManager;

        public ScDeviceJob(ScriptManager scriptManager, ITaskSvr taskSvr)
        {
            _scriptManager = scriptManager;
            _taskSvr = taskSvr;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            var dataMap = context.JobDetail.JobDataMap;
            _scTaskId = dataMap.GetInt(ScTaskId);
            _device = dataMap.GetString(Device);

            // 检索任务详情
            var scTask = await _taskSvr.GetTask(_scTaskId);

            if (scTask.SubscriptionRange == ScSubscriptionRange.All)
            {
                // 采集此设备中所关注的公众号, 完毕之后启动公众号消息采集任务
                await _scriptManager.RunFetchSubscriptionsExecutor(_scTaskId, _device);
            }
            else
            {
                // 启动公众号消息采集任务
                await _scriptManager.RunFetchSubscriptionMsgsExecutor(_scTaskId, _device);
            }
        }
    }
}
